<?php
/**
 * 教室信息类
 */
namespace app\admin\controller;

use app\admin\model\Usermodel;
use think\App;
use think\facade\View;
use app\admin\model\Teachermodel;
use app\admin\model\Classroom as croom;
use app\admin\validate\Classroom as validate;

class Teacher extends Common
{
    protected $croommodel;
    protected $userModel;

    public function __construct(App $app)
    {
        $this->validate = validate::class;
        $this->localModel = new Teachermodel();
        parent::__construct($app);
        $this->croommodel = new croom();
        $this->userModel = new Usermodel();
    }

    public function index()
    {
        $where = $showDataArr = $croomWhereData = [];

        $dataArr = $this->request->request('search/a', []);

        if($dataArr['nickname'])  $where[] = ['t.nickname', 'like', "%".$dataArr['nickname']."%"];
        if($dataArr['username'])  $where[] = ['u.username', 'like', "%".$dataArr['username']."%"];
        if($dataArr['cid'])       $where[] = ['u.cid', '=', $dataArr['cid']];
        if($dataArr['status'])    $where[] = ['u.status', '=', $dataArr['status']];
        if(session('adminUserInfo.rid') == 7){
            //代理管理员只能看到自己管理下的教室信息
            $croomWhereData[] = ['agent_uid', '=', session('adminUserInfo.uid')];
        }
        if(session('adminUserInfo.cid')){
            $where[] = ['u.cid', '=', session('adminUserInfo.cid')];
        }
        $croomData = $this->croommodel->findAllInfo('id, name', $croomWhereData);
        if($croomData && session('adminUserInfo.rid') == 7){
            foreach($croomData as $croomVal){
                $ids[] = $croomVal['id'];
            }
            $where[] = ['u.cid', 'in', implode(',', $ids)];
        }

        $list = $this->localModel->findAll($where, $this->request->param());
        $showDataArr = ['data'=>$list['data'], 'pageData'=>$list['page'], 'dataArr'=>$dataArr, 'page'=>$this->request->get('page'), 'croomData'=>$croomData];
        View::assign(array_merge($dataArr, $showDataArr));
        return view();
    }

    public function add(){
        $croomData = $this->croommodel->findAllInfo('id, name', [['status', '=', 1]]);

        $id = $this->request->get('id', 0);

        $showData = ['croomData'=>$croomData];
        if($id){
            $showData['id'] = $id;
            $showData['detail'] = $this->localModel->findDetaiById($id);
            View::assign($showData);
            return view('edit');
        }else{
            View::assign($showData);
            return view();
        }
    }

    public function save(){
        $userArr = $this->request->request('user/a', []);
        $dataArr = $this->request->request('info/a', []);
        $id = $this->request->post('id', 0);
        if(!$userArr['password'])  unset($userArr['password']);

        if($msg = $this->checkData(array_merge($dataArr, $userArr), session('adminUserInfo.cid') ? ($id ? 'no_teacher' : 'teacher') : ($id ? 'no_teachers' : 'teachers')))  return json(['result'=>'fail', 'title'=>$msg]);

        if($this->localModel->getteacherInfoByName($dataArr['nickname'], $id))  return json(['result'=>'fail', 'title'=>'教师名字重复！']);
        //if($this->userModel->getUserInfoByName($userArr['username'], $dataArr['tuid']))  return json(['result'=>'fail', 'title'=>'该教师登录账号已存在！']);

        $res = $this->localModel->saveTeacherInfo($dataArr, $userArr, $id);

        if(!$res)  return json(['result'=>'fail', 'title'=>'操作失败！']);

        $this->logsData = ['cid'=>$id ? $id : $res, 'action'=>$id ? 'edit' : 'add', 'msg'=>$dataArr['nickname']];
        return json(['result'=>'success', 'title'=>'恭喜您操作成功！', 'url'=>(string)url('Teacher/index')]);
    }

    public function ajaxGetTeacher($cid){
        if(!$cid)  return json(['type'=>'fail', 'msg'=>'cid不能为空']);

        $res = $this->localModel->findAll([['u.status', '=', 1], ['u.cid', '=', $cid]], $this->request->param(), 100);
        return json(['result'=>'success', 'date'=>$res['data']]);
    }


}
